#!/opt/bin/python
import sys, os
import cgi
import cgitb; cgitb.enable()
from Cookie import SimpleCookie

from ttemplate import TTemplate
import aws

class State():
    def __init__(self):
        # Read Post & Cookie Data
        self.cgi = cgi
        self.form = cgi.FieldStorage()
        self.cook = SimpleCookie(os.environ.get('HTTP_COOKIE'))
        self.tmpl = TTemplate('html/')
        self.request_uri = os.environ.get('REQUEST_URI')
        self.request_url = self.make_url(self.request_uri)
        sn = os.environ.get('SCRIPT_NAME', '')
        if len(sn) > 1: (sn, junk) = sn.rsplit('/',1)
        self.request_base_uri = sn + '/'
        self.request_base_url = self.make_url(self.request_base_uri)
        # Verify logged in here...
            
    def make_url(self,uri='/'):
        server_name = os.environ.get('SERVER_NAME')
        server_port = os.environ.get('SERVER_PORT')
        server_scheme = 'http'
        if server_port == '80':
            server_port = ""
        else:
            server_port = ':%s' % server_port
        url = "%s://%s%s%s" % (server_scheme, server_name, server_port, uri)
        return url

